<?php

function morra( $var ) {
    die( '<pre>' . print_r($var , 1) );
}

function debug( $var ) {
    echo '<pre>';
    print_r($var);
}

function show( $string ) {
    return htmlentities(Encode::forceUTF8($string), ENT_QUOTES, 'UTF-8');
}

function dateBDtoBR( $DtDate ) {
    if(!$DtDate || $DtDate == '0000-00-00') {return null;}
    $ArDados = explode('-',$DtDate);
    return $ArDados[2] . '/' . $ArDados[1] . '/' . $ArDados[0];
}

function dateBRtoBD( $DtDate ) {
    if(!$DtDate || $DtDate == '00/00/000') {return null;}
    $ArDados = explode('/',$DtDate);
    return $ArDados[2] . '-' . $ArDados[1] . '-' . $ArDados[0];
}

function datetimeBRtoBD( $DtDate ) {
    if(!$DtDate || $DtDate == '00/00/000 00:00:00') {return null;}
    $ArDateAndTime = explode(' ', $DtDate);
    return dateBRtoBD($ArDateAndTime[0]) . ' ' . $ArDateAndTime[1];
}

function datetimeBDtoBR( $DtDate ) {
    if(!$DtDate || $DtDate == '0000-00-00 00:00:00') {return null;}
    $ArDateAndTime = explode(' ', $DtDate);
    return dateBDtoBR($ArDateAndTime[0]) . ' ' . $ArDateAndTime[1];
}

function dateDiff($start_date, $end_date) {
    $date1 = strtotime($start_date);
    $date2 = strtotime($end_date);

    return array(
	  'day' => round((($date2 - $date1)/DAY)),
	  'hour' => round((($date2 - $date1)/HOUR)%24),
          'minute' => round((($date2 - $date1)/MINUTE)%60),
	  'second' => round(($date2 - $date1)%60)
    );
}

function multipleEntities( &$ArHaystack ) {
    if( !$ArHaystack ) { return false; }
    if( is_array($ArHaystack) ) {
        foreach( $ArHaystack as &$MxHaystack ) {
            if( is_array($MxHaystack) ) {
                multipleEntities($MxHaystack);
                continue;
            }
            $MxHaystack = htmlentities($MxHaystack, ENT_QUOTES, 'UTF-8');
        }
    } else {
        $ArHaystack = htmlentities($ArHaystack, ENT_QUOTES, 'UTF-8');
    }
}

function hasItem($array) {
    if( !$array ) { return false; }
    return count($array) > 0 ? true : false;
}

function isFerramentaHabilitada( $ArFerramentasHabilitadas, $Ferramenta ) {
   if( empty($ArFerramentasHabilitadas) ) {
        return false;
    }
    foreach($ArFerramentasHabilitadas as $FerramentaHabilitada) {
        if( $FerramentaHabilitada->equals($Ferramenta) ) {
            return true;
        }
    }
    return false;
}

function now() {
    return date('Y-m-d H:i:s');
}

function addDayIntoDate($DtDate,$ItDays) {

     $thisYear = substr ( $DtDate, 0, 4 );
     $thisMonth = substr ( $DtDate, 5, 2 );
     $thisDay =  substr ( $DtDate, 8, 2 );
     $nextDate = mktime ( 0, 0, 0, $thisMonth, $thisDay + $ItDays, $thisYear );
     return strftime("%Y-%m-%d", $nextDate);
}

function today($StLocale = 'pt-BR') {
    return $StLocale == 'pt-BR' ? date('d/m/Y') : date('Y-m-d');
}

function loga($string) {
    $content = file_get_contents('log.txt') . "\n" . $string;
    $f = fopen('log.txt', 'w+');
    fwrite($f, $content, strlen($content));
    fclose($f);
}